<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller {

    private static $layout_data = array();
    private static $cart_data = array();

    public function __construct()
    {
        parent::__construct();

        $this->layout_data['pageTitle'] = 'Hoa Khô Tường Vi - Shop online - Hoa - Bán Hoa';
        $this->layout_data['navTab'] = 'cart';

        //Load library cart
        $this->load->library('cart');

    }

    /**
     * Index function
     */
    public function index()
    {

        $this->cart_data = '';

        //Set layout content
        $this->layout_data['content'] = $this->load->view('cart/index', $this->cart_data, TRUE);

        //render to layout
        $this->load->view('layouts/main', $this->layout_data);
    }

    /**
     * Add product to cart
     */
    public function addToCart()
    {
        //Get productId
        $productId = intval($this->input->post('pid'));

        //Set response default
        $response = array(
            'error'     =>  1,
            'message'   =>  'Product not found'
        );

        //Load model product
        $this->load->model('product_model', 'productModel');

        //Get product detail
        $productDetail = $this->productModel->getProductDetail($productId);

        //Check product detail
        if(empty($productDetail))
        {
            //return data
            echo json_encode($response); exit();
        }

        //Check sale
        $price = $productDetail['Status']==2 ? $productDetail['PriceSale'] : $productDetail['Price'];

        //Set cart data
        $cartData = array(
            'id'      => $productDetail['ProductID'],
            'qty'     => 1,
            'price'   => $price,
            'name'    => $productDetail['Name'],
            'options' => array('Code' => $productDetail['Code'])
            );

        //Add to cart
        $this->cart->insert($cartData);

        //Set response again
        $response = array(
            'error'     =>  0,
            'message'   =>  'Success'
        );

        //return data
        echo json_encode($response); exit();
    }
}

/* End of file Home.php */
/* Location: ./application/controllers/welcome.php */